package com.lemner.ke.activity;

import android.graphics.drawable.GradientDrawable;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemner.ke.R;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.base.BaseObjectBean;
import com.lemner.ke.base.SuccessListener;
import com.lemner.ke.bean.LoginBean;
import com.lemner.ke.model.fail.FailModel;
import com.lemner.ke.model.loginreg.GetIdentifyingCodeModel;
import com.lemner.ke.model.loginreg.LoginModel;
import com.lemner.ke.util.Phone;
import com.lemner.ke.util.SpUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.OnTextChanged;

/**

* 作者：张恺

* 时间：2018/9/25

* 类描述：登陆页面

*/

public class LoginActivity extends BaseActivity {
    private int flag = 0, time;
    private RelativeLayout login_topbar;
    private ImageView topbar_back, iv_clear;
    private Button btn_login, btn_reg;
    private TextView tv_left, tv_right, tv_forget, tv_send, tv_user_protocol, tv_protection_clause;
    private EditText et_phone, et_password, et_verification_code;
    private LinearLayout ll_verification_code;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        login_topbar = findViewById(R.id.login_topbar);

        topbar_back = findViewById(R.id.iv_login_back);
        iv_clear = findViewById(R.id.iv_login_clear);

        btn_login = findViewById(R.id.btn_login);
        btn_reg = findViewById(R.id.btn_login_reg);

        tv_left = findViewById(R.id.tv_login_left);
        tv_right = findViewById(R.id.tv_login_right);
        tv_forget = findViewById(R.id.tv_login_forget);
        tv_send = findViewById(R.id.tv_verification_send);
        tv_user_protocol = findViewById(R.id.tv_user_protocol);
        tv_protection_clause = findViewById(R.id.tv_protection_clause);

        et_phone = findViewById(R.id.et_login_phone);
        et_password = findViewById(R.id.et_login_password);
        et_verification_code = findViewById(R.id.et_verification_code);
        ll_verification_code = findViewById(R.id.ll_verification_code);
    }

    @Override
    public void initEvent() {
        topbar_back.setOnClickListener(this);
        iv_clear.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        tv_forget.setOnClickListener(this);
        tv_send.setOnClickListener(this);
        tv_user_protocol.setOnClickListener(this);
        tv_protection_clause.setOnClickListener(this);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!et_phone.getText().toString().equals("")){
                    btn_login.setBackgroundResource(R.drawable.change_phone_button);
                    iv_clear.setVisibility(View.VISIBLE);
                }else {
                    btn_login.setBackgroundResource(R.drawable.setting_button);
                    iv_clear.setVisibility(View.GONE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initSetting() {
        setTopBarEnable(false);
    }

    @Override
    public void onClick(View v) {
        final String mobile = et_phone.getText().toString();
        final String password = et_password.getText().toString();
        final String code=et_verification_code.getText().toString();
        switch (v.getId()) {
            case R.id.iv_login_back:
                finish();
                break;
            case R.id.iv_login_clear:
                et_phone.setText("");
                break;
            case R.id.btn_login:
                if (flag == 0) {
                    if (mobile.isEmpty()) {
                        showToast("请输入手机号！");
                    } else if (password.isEmpty()) {
                        showToast("请输入密码！");
                    } else if (password.length() < 6) {
                        showToast("密码长度不能少于6位！");
                    } else if (Phone.isMobile(mobile))
                        new LoginModel().login(mobile,password, new BaseListener<BaseObjectBean<LoginBean>>() {
                        @Override
                        public void onResponse(BaseObjectBean<LoginBean> loginBeanBaseObjectBean) {
                            SpUtils.putString(getContext(), SpUtils.USERIDS, loginBeanBaseObjectBean.getData().getIds());
                            SpUtils.putString(LoginActivity.this, SpUtils.MOBILE_PHOE, loginBeanBaseObjectBean.getData().getMobile());
                            SpUtils.putInt(LoginActivity.this, SpUtils.LOGIN_STATUS, SpUtils.LOGIN);
                            hideSoftKeyBoard(LoginActivity.this);
                            finish();
                        }

                        @Override
                        public void onFail(String msg) {

                        }
                    });
                }if (flag == 1) {
                if (mobile.isEmpty()) {
                    showToast("请输入手机号！");
                } else if (code.isEmpty()) {
                    showToast("请输入验证码！");
                } else if (Phone.isMobile(mobile)) {
                    new LoginModel().loginMobile(mobile, code, new BaseListener<BaseObjectBean<LoginBean>>() {
                        @Override
                        public void onResponse(BaseObjectBean<LoginBean> stringBaseObjectBean) {
                            showToast(stringBaseObjectBean.getMsg());
                            // 存储登录成功的userId与登录状态
                            String userId = stringBaseObjectBean.getData().getIds();
                            SpUtils.putString(LoginActivity.this, SpUtils.USERIDS, userId);
                            SpUtils.putString(LoginActivity.this, SpUtils.MOBILE_PHOE, stringBaseObjectBean.getData().getMobile());
                            SpUtils.putInt(LoginActivity.this, SpUtils.LOGIN_STATUS, SpUtils.LOGIN);
                            hideSoftKeyBoard(LoginActivity.this);
                            finish();
                        }

                        @Override
                        public void onFail(String msg) {
                            new FailModel().login(mobile, password, new BaseListener<BaseObjectBean<String>>() {
                                @Override
                                public void onResponse(BaseObjectBean<String> stringBaseObjectBean) {
                                    showToast(stringBaseObjectBean.getMsg());
                                }

                                @Override
                                public void onFail(String msg) {
                                    showToast(msg);
                                }
                            });
                        }
                    });
                } else {
                    showToast("非法手机号，请重新输入！");
                }
                }
                break;
            case R.id.btn_login_reg:
                startActivity(RegisterActivity.class);
                break;
            case R.id.tv_login_left:
                flag = 0;
                tv_right.setTextSize(13);
                tv_left.setTextSize(16);
                tv_left.setTextColor(0xff111111);
                tv_right.setTextColor(0xff666666);
                ll_verification_code.setVisibility(View.GONE);
                et_password.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_login_right:
                flag = 1;
                tv_right.setTextSize(16);
                tv_left.setTextSize(13);
                tv_left.setTextColor(0xff666666);
                tv_right.setTextColor(0xff111111);
                et_password.setVisibility(View.GONE);
                ll_verification_code.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_login_forget:
                startActivity(ForgetPasswordActivity.class);
                break;
            case R.id.tv_verification_send:
                if (mobile.isEmpty()) {
                    showToast("请输入手机号！");
                } else if (!Phone.isMobile(mobile)) {
                    showToast("非法手机号，请重新输入！");
                }else {
                    new GetIdentifyingCodeModel().getIdentifyingCode(mobile, "identity", new SuccessListener());
                    time = 60;
                    final Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        if (time <= 0) {
                                            tv_send.setText("发送验证码");
                                            tv_send.setClickable(true);
                                            timer.cancel();
                                        } else {
                                            tv_send.setText("重新发送(" + time-- + "s)");
                                            tv_send.setClickable(false);
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    }, 0, 1000);
                }
                break;
//            case R.id.tv_user_protocol:
//
//                break;
//            case R.id.tv_protection_clause:
//
//                break;
                }
        }
    }
